package com.cx.core.magazine.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;

import com.cx.core.magazine.dao.ImgresourceDao;
import com.cx.core.magazine.util.MagTool;
import com.li.core.daoBase.DAOBase;

public class ImgresourceDaoImpl extends DAOBase implements ImgresourceDao {
	private LobHandler lobHandler;

	public void setLobHandler(LobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}

	@Override
	public String queryImage(int imageid) throws Exception {
		List<String> returnpath = new ArrayList<String>();
		Integer[] pars = new Integer[] { imageid };
		class ImageResultSetExtrator extends
				AbstractLobStreamingResultSetExtractor {
			List<String> returnpath = null;

			public ImageResultSetExtrator(List<String> rp) {
				this.returnpath = rp;
			}

			@Override
			protected void streamData(ResultSet rs) throws SQLException,
					IOException, DataAccessException {
				String filename = null;
				filename = rs.getString("image_id") + "_"
						+ rs.getString("name");
				try {
					returnpath.add(MagTool.getMagTool().saveFileTolocal(
							filename, rs.getBinaryStream("resource"), true));
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

		}

		this.getJdbcT()
				.query("select image_id,name,resource from imgresource where image_id=?",
						pars, new ImageResultSetExtrator(returnpath));
		// SqlRowSet rs = this.getJdbcT().queryForRowSet(
		// "select image_id,name,resource from test where image_id=?",
		// pars);
		// while (rs.next()) {
		//
		//
		// rs.getBinaryStream("resource"), false));
		// }
		StringBuffer result = new StringBuffer();
		for (String path : returnpath) {
			result.append(path);
			result.append(",");
		}
		return result.substring(0, result.length() - 1);
	}

	@Override
	public Blob queryImageBlob(int imageid) throws Exception {
		return null;
	}

	@Override
	public int saveBlob(Blob blob) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public int saveImage(String name, byte[] bytes) throws Exception {
		final String fname = name;
		final byte[] fbytes = bytes;
		
		String sql = " INSERT INTO imgresource(name,resource)" + " VALUES(?,?)";
		this.getJdbcT().execute(
				sql,
				new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
					@Override
					protected void setValues(PreparedStatement ps,
							LobCreator lobCreator) throws SQLException,
							DataAccessException {
						ps.setString(1, fname);
						lobCreator.setBlobAsBytes(ps, 2, fbytes);
					}
				});
		int id = this.getJdbcT().queryForInt("select @@identity");
		return id;
	}
	
	@Override
	public void deleteImage(String image_id) throws Exception {
		Object[] pars = new Object[]{ image_id };
		String sql = "delete from imgresource where image_id=?";
		this.getJdbcT().update(sql,pars);
	}
	
	public static void main(String[] args) throws Exception {
		ImgresourceDaoImpl id = new ImgresourceDaoImpl();
		FileInputStream fi = new FileInputStream(new File("e://1.jpg"));
		MagTool.getMagTool().saveFileTolocal("网.jpg", fi, true);
	}
}
